/**
 * created by 小卷毛, 2022-03-01
 * Copyright (c) 2022, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
@file:Suppress("unused")

package com.rzico.sbl.other

import android.graphics.Color
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.text.style.ImageSpan
import android.text.style.RelativeSizeSpan
import android.text.style.StyleSpan
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import com.github.mikephil.charting.charts.BarChart
import com.github.mikephil.charting.charts.LineChart
import com.github.mikephil.charting.charts.PieChart
import com.github.mikephil.charting.components.Legend
import com.github.mikephil.charting.components.XAxis.XAxisPosition
import com.github.mikephil.charting.components.YAxis
import com.github.mikephil.charting.components.YAxis.YAxisLabelPosition
import com.github.mikephil.charting.data.*
import com.github.mikephil.charting.formatter.IValueFormatter
import com.rzico.sbl.R
import com.xinnuo.common.extend.getColorEx
import com.xinnuo.common.extend.getDrawableEx
import com.xinnuo.common.helper.dp2px
import java.math.BigDecimal

/**
 * 描 述：图表控件初始化扩展方法
 * 作 者：小卷毛  2022-03-01 17:24
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */

/**
 * 初始化 PieChart
 */
fun <T : PieChart> T.initChart(
    isLegend: Boolean = true,
    xSpaceEntry: Float = 50f,
    ySpaceEntry: Float = 5f
) {
    setUsePercentValues(false)    //图块值是否为百分比
    description.isEnabled = false //是否显示描述组件
    extraTopOffset = 5f           //顶部偏移量
    isRotationEnabled = true      //是否可以手动旋转
    isDragDecelerationEnabled = true //启用/禁用减速摩擦效果，默认true
    // 减速摩擦系数在[0,1]区间内，较高的值表示速度会缓慢下降，
    // 例如如果设置为0，它将立即停止。1是无效值，将自动转换为0.9999
    dragDecelerationFrictionCoef = 0.95f
    isHighlightPerTapEnabled = true //是否点击高亮色
    setDrawEntryLabels(false)       //是否显示每个方块标签

    isDrawHoleEnabled = true        //是否为同心圆
    setHoleColor(Color.TRANSPARENT) //内圆颜色
    holeRadius = 50f                //内圆半径，百分比

    setTransparentCircleAlpha(0)                 //透明圆的透明度
    setTransparentCircleColor(Color.TRANSPARENT) //透明圆颜色
    transparentCircleRadius = 50f                //透明圆半径，百分比

    setDrawCenterText(true)    //是否设置中间富文本
    centerTextSpacingadd = 16f //富文本行间距

    rotationAngle = 270f //初始动画旋转角度

    // 无数据时设置
    setNoDataText("暂无相关数据")
    setNoDataTextColor(context.getColorEx(R.color.light))

    // 图例展示设置
    legend.apply {
        isEnabled = isLegend                                          //是否显示图例
        verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM     //垂直相对位置
        horizontalAlignment = Legend.LegendHorizontalAlignment.CENTER //水平相对
        orientation = Legend.LegendOrientation.HORIZONTAL             //排列方向
        isWordWrapEnabled = true                                      //设置是否启用自动换行
        setDrawInside(false) //图例是否展示在内部或外部
        formSize = 11f       //图例形状尺寸，dp
        formToTextSpace = 7f //形状和文本之间的距离
        textSize = 12f       //文本字体大小，dp
        textColor = context.getColorEx(R.color.black) //文本字体颜色
        xEntrySpace = xSpaceEntry //图例水平间距，dp
        yEntrySpace = ySpaceEntry //图例垂直间距，dp
        yOffset = 10f             //图例垂直向下偏移，dp
    }
}

/**
 * 初始化 BarChart
 */
fun <T : BarChart> T.initChart(
    isLegend: Boolean = true,
    isFitBars: Boolean = false,
    touchDistance: Float = 5f,
    isCenterLabel: Boolean = false,
    isXGridLine: Boolean = false,
    isYGridLine: Boolean = true,
    xLabelAngle: Float = 0f,
    xLabelCount: Int = 7,
    yLabelCount: Int = 6,
    xTextSize: Float = 10f,
    yTextSize: Float = 10f,
    xAxisMinimum: Float = -1f,
    yAxisMinimum: Float = -1f,
    xSpaceEntry: Float = 30f,
    ySpaceEntry: Float = 5f
) {
    extraBottomOffset = 15f          //底部偏移量
    description.isEnabled = false    //是否显示描述组件
    isScaleXEnabled = true           //启用/禁用x轴上的缩放
    isScaleYEnabled = true           //启用/禁用y轴上的缩放
    isDoubleTapToZoomEnabled = false //启用/禁用通过双击放大图表
    setDrawBarShadow(false)          //是否显示阴影
    setDrawValueAboveBar(true)       //设置Bar值在顶部上方或下方
    setDrawGridBackground(false)     //启用/禁用图表背景色
    setPinchZoom(false)              //启用/禁用捏合缩放。如果禁用，x轴和y轴可以单独缩放
    setMaxVisibleValueCount(31)      //同时显示Bar值的个数，超过不再显示
    setFitBars(isFitBars)            //X轴是否自适应所有柱形图
    maxHighlightDistance = touchDistance //触发highlight点击偏离距离

    // 无数据时设置
    setNoDataText("暂无相关数据")
    setNoDataTextColor(context.getColorEx(R.color.light))

    // 设置x轴属性
    xAxis.also {
        it.position = XAxisPosition.BOTTOM //x轴位置
        it.setDrawAxisLine(true)     //是否显示轴条
        it.labelCount = xLabelCount  //设置轴标签个数
        it.textSize = xTextSize                             //文本大小，dp
        it.textColor = context.getColorEx(R.color.black)    //文本颜色
        it.axisLineColor = context.getColorEx(R.color.gray) //线条颜色
        it.labelRotationAngle = xLabelAngle   //标签旋转角度
        it.granularity = 1f                   //轴值之间的最小间隔
        it.setCenterAxisLabels(isCenterLabel) //设置柱子（柱子组）居中对齐x轴上的点
        it.setDrawGridLines(isXGridLine)                                   //是否显示表格线条
        it.enableGridDashedLine(5f, 5f, 0f) //网格线虚线模式
        if (xAxisMinimum >= 0) it.axisMinimum = xAxisMinimum               //x轴最小值
    }

    // 设置左y轴属性
    axisLeft.also {
        it.setPosition(YAxisLabelPosition.OUTSIDE_CHART)    //设置轴标签显示位置
        it.textSize = yTextSize                             //文本大小，dp
        it.textColor = context.getColorEx(R.color.black)    //文本颜色
        it.axisLineColor = context.getColorEx(R.color.gray) //线条颜色
        it.labelCount = yLabelCount   //设置轴标签个数
        it.granularity = 1f           //轴值之间的最小间隔
        it.setDrawGridLines(isYGridLine)                                   //是否显示表格线条
        it.enableGridDashedLine(5f, 5f, 0f) //网格线虚线模式
        if (yAxisMinimum >= 0) it.axisMinimum = yAxisMinimum               //y轴最小值
    }

    // 隐藏右y轴属性
    axisRight.isEnabled = false

    // 图例展示设置
    legend.apply {
        isEnabled = isLegend                                          //是否显示图例
        verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM     //垂直相对位置
        horizontalAlignment = Legend.LegendHorizontalAlignment.CENTER //水平相对
        orientation = Legend.LegendOrientation.HORIZONTAL             //排列方向
        isWordWrapEnabled = true                                      //设置是否启用自动换行
        setDrawInside(false) //图例是否展示在内部或外部
        formSize = 11f       //图例形状尺寸，dp
        formToTextSpace = 7f //形状和文本之间的距离
        textSize = 12f       //文本字体大小，dp
        textColor = context.getColorEx(R.color.black) //文本字体颜色
        xEntrySpace = xSpaceEntry //图例水平间距，dp
        yEntrySpace = ySpaceEntry //图例垂直间距，dp
        yOffset = 10f             //图例垂直向下偏移，dp
    }
}

/**
 * 初始化商品页面 LineChart
 */
fun <T : LineChart> T.initChart(
    touchDistance: Float = 10f,
    isXGridLine: Boolean = false,
    isYGridLine: Boolean = true,
    xLabelAngle: Float = 0f,
    xLabelCount: Int = 7,
    yLabelCount: Int = 6,
    xTextSize: Float = 10f,
    yTextSize: Float = 10f,
    xSpaceRatio: Float = 0f,
    isLegend: Boolean = false,
    xAxisMinimum: Float = -1f,
    yAxisMinimum: Float = -1f,
    xSpaceEntry: Float = 30f,
    ySpaceEntry: Float = 5f
) {
    extraBottomOffset = 15f          //底部偏移量
    description.isEnabled = false    //是否显示描述组件
    setDrawGridBackground(false)     //启用/禁用图表背景色
    isScaleXEnabled = true           //启用/禁用x轴上的缩放
    isScaleYEnabled = true           //启用/禁用y轴上的缩放
    isDoubleTapToZoomEnabled = false //启用/禁用通过双击放大图表
    setPinchZoom(false)              //启用/禁用捏合缩放。如果禁用，x轴和y轴可以单独缩放
    maxHighlightDistance = touchDistance //触发highlight点击偏离距离

    // 无数据时设置
    setNoDataText("暂无相关数据")
    setNoDataTextColor(context.getColorEx(R.color.light))

    // 设置x轴属性
    xAxis.also {
        it.position = XAxisPosition.BOTTOM //x轴位置
        it.setDrawAxisLine(true)           //是否显示轴条
        it.labelCount = xLabelCount        //设置轴标签个数
        it.textSize = xTextSize                             //文本大小，dp
        it.textColor = context.getColorEx(R.color.black)    //文本颜色
        it.axisLineColor = context.getColorEx(R.color.gray) //线条颜色
        it.setAvoidFirstLastClipping(true)  //避免首尾标签被剪掉
        it.granularity = 1f                 //轴值之间的最小间隔
        it.labelRotationAngle = xLabelAngle //标签旋转角度
        it.spaceMin = xSpaceRatio           //x轴不从0点开始，偏移比例
        it.spaceMax = xSpaceRatio           //x轴到最后，偏移比例
        it.setDrawGridLines(isXGridLine)                                   //是否显示表格线条
        it.enableGridDashedLine(5f, 5f, 0f) //网格线虚线模式
        if (xAxisMinimum >= 0) it.axisMinimum = xAxisMinimum               //x轴最小值
    }

    // 设置左y轴属性
    axisLeft.also {
        it.setPosition(YAxisLabelPosition.OUTSIDE_CHART)    //设置轴标签显示位置
        it.textSize = yTextSize                             //文本大小，dp
        it.textColor = context.getColorEx(R.color.black)    //文本颜色
        it.axisLineColor = context.getColorEx(R.color.gray) //线条颜色
        it.labelCount = yLabelCount //设置轴标签个数
        it.granularity = 1f         //轴值之间的最小间隔
        it.setDrawGridLines(isYGridLine)                                   //是否显示表格线条
        it.enableGridDashedLine(5f, 5f, 0f) //网格线虚线模式
        if (yAxisMinimum >= 0) it.axisMinimum = yAxisMinimum               //y轴最小值
    }

    // 设置右y轴属性
    axisRight.also {
        it.isEnabled = false //不显示右侧y轴
        // it.setLabelCount(6, false)
        // it.setDrawGridLines(false)
        // it.setDrawLabels(false)
        // it.axisMinimum = 0f
        // it.axisLineColor = getColorEx(R.color.gray)
    }

    // 图例展示设置
    legend.apply {
        isEnabled = isLegend                                          //是否显示图例
        verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM     //垂直相对位置
        horizontalAlignment = Legend.LegendHorizontalAlignment.CENTER //水平相对
        orientation = Legend.LegendOrientation.HORIZONTAL             //排列方向
        isWordWrapEnabled = true                                      //设置是否启用自动换行
        setDrawInside(false) //图例是否展示在内部或外部
        formSize = 11f       //图例形状尺寸，dp
        formToTextSpace = 7f //形状和文本之间的距离
        textSize = 12f       //文本字体大小，dp
        textColor = context.getColorEx(R.color.black) //文本字体颜色
        xEntrySpace = xSpaceEntry //图例水平间距，dp
        yEntrySpace = ySpaceEntry //图例垂直间距，dp
        yOffset = 10f             //图例垂直向下偏移，dp
    }
}

/**
 * 设置 PieChart 中心图片
 *
 * setSpan的Flag参数表示：start和end是开区间还是闭区间。
 * Spanned.SPAN_EXCLUSIVE_EXCLUSIVE —— (a,b)
 * Spanned.SPAN_EXCLUSIVE_INCLUSIVE —— (a,b]
 * Spanned.SPAN_INCLUSIVE_EXCLUSIVE —— [a,b)
 * Spanned.SPAN_INCLUSIVE_INCLUSIVE —— [a,b]
 */
fun <T : PieChart> T.setCenterImage(
    @DrawableRes resId: Int,
    drawableSize: Float = 40f
) {
    centerText =
        SpannableString(" ")
            .apply {
                // 文本插入图片
                setSpan(
                    ImageSpan(
                        context.getDrawableEx(resId).apply {
                            setBounds(
                                0, 0,
                                dp2px(drawableSize),
                                dp2px(drawableSize)
                            )
                        }
                    ), 0, 1, 0
                )
            }
}

/**
 * 设置 PieChart 中心文本
 *
 * setSpan的Flag参数表示：start和end是开区间还是闭区间。
 * Spanned.SPAN_EXCLUSIVE_EXCLUSIVE —— (a,b)
 * Spanned.SPAN_EXCLUSIVE_INCLUSIVE —— (a,b]
 * Spanned.SPAN_INCLUSIVE_EXCLUSIVE —— [a,b)
 * Spanned.SPAN_INCLUSIVE_INCLUSIVE —— [a,b]
 */
fun <T : PieChart> T.formartCenterText(
    value: String,
    hint: String,
    proportion: Float = 1.6f,
    sort: String = "left2right",
    @ColorRes valueColor: Int = R.color.black,
    @ColorRes hintColor: Int = R.color.black,
) {
    centerText = when (sort) {
        "left2right" ->
            SpannableString("$value\n$hint").apply {
                // 设置文字相对大小，指相对于文本设定的大小的相对比例
                setSpan(RelativeSizeSpan(proportion), 0, value.length, 0)
                // 文字设置样式（正常、粗体、斜体、粗斜体）
                setSpan(StyleSpan(Typeface.BOLD), 0, value.length, 0)
                // 设置文字颜色
                setSpan(ForegroundColorSpan(context.getColorEx(valueColor)), 0, value.length, 0)
                setSpan(ForegroundColorSpan(context.getColorEx(hintColor)), value.length, length, 0)
            }
        "right2left" ->
            SpannableString("$hint\n$value").apply {
                // 设置文字相对大小，指相对于文本设定的大小的相对比例
                setSpan(RelativeSizeSpan(proportion), hint.length, length, 0)
                // 文字设置样式（正常、粗体、斜体、粗斜体）
                setSpan(StyleSpan(Typeface.BOLD), hint.length, length, 0)
                // 设置文字颜色
                setSpan(ForegroundColorSpan(context.getColorEx(hintColor)), 0, hint.length, 0)
                setSpan(ForegroundColorSpan(context.getColorEx(valueColor)), hint.length, length, 0)
            }
        else -> ""
    }
}

/**
 * 设置 PieChart 数据
 */
fun <T : PieChart> T.setPieData(
    colors: List<Int>,
    formatter: IValueFormatter? = null,
    vararg entries: PieEntry
) {
    data = PieData(
        PieDataSet(entries.toList(), "").apply {
            sliceSpace = 2f     //饼状间隙，dp
            selectionShift = 8f //饼状点击突出距离，dp
            setColors(colors)   //设置饼状图块颜色

            yValuePosition = PieDataSet.ValuePosition.OUTSIDE_SLICE //图块值显示位置，内或外
            valueLinePart1OffsetPercentage = 90f //图块值相对图块偏移百分比
            valueLinePart1Length = 0.4f          //图块值第一部分线条长度比
            valueLinePart2Length = 0.6f          //图块值第二部分线条长度比
            setUseValueColorForLine(true)        //图块值线条颜色是否跟图库一致
        }
    ).apply {
        setValueTextSize(13f)        //图块描述文本大小
        setValueTextColors(colors)   //图块描述文本颜色
        setValueFormatter(formatter) //图块描述文本格式
    }
}

/**
 * 设置单组 BarChart 数据
 */
fun <T : BarChart> T.setSingleData(
    entries: List<BarEntry>,
    colorList: List<Int>,
    isHighlight: Boolean = true,
    barWidthRatio: Float = 0.4f,
    valueSize: Float = 10f,
    valueColor: Int = context.getColorEx(R.color.black),
    formatter: IValueFormatter? = null
) {
    if (data != null && data.dataSetCount > 0) {
        (data.getDataSetByIndex(0) as BarDataSet).entries = entries
        (data.getDataSetByIndex(0) as BarDataSet).colors = colorList
        data.notifyDataChanged()
        notifyDataSetChanged()
    } else {
        data = BarData(
            BarDataSet(entries, "")
                .setProperty(
                    colorList = colorList,
                    isHighlight = isHighlight,
                    valueSize = valueSize,
                    valueColor = valueColor,
                    formatter = formatter
                )
        ).apply {
            barWidth = barWidthRatio //柱块宽度比例
        }
    }

    // 设置为可以左右滑动，放大x轴倍数
    // viewPortHandler.refresh(Matrix().apply { postScale(1.5f, 1f) }, this, false)
}

/**
 * 设置分组 BarChart 数据
 *
 * @param entries       组数据
 * @param colorList     组颜色值
 * @param labelList     组图例文本
 * @param isHighlight   是否高亮
 * @param barWidthRatio 默认柱宽比例
 * @param xFromValue    起始值，等于第一条数据索引
 * @param valueSize     参考值文本
 */
fun <T : BarChart> T.setGroupData(
    entries: List<List<BarEntry>>,
    colorList: List<Int>,
    labelList: List<String> = ArrayList(),
    isHighlight: Boolean = false,
    highColor: Int = Color.TRANSPARENT,
    barWidthRatio: Float = 0.4f,
    xFromValue: Float = 1f,
    valueSize: Float = 10f,
    formatter: IValueFormatter? = null
) {
    var groupSpace = 0f

    data = BarData(
        entries.mapIndexed { index, list ->
            BarDataSet(list, if (labelList.size > index) labelList[index] else "")
                .setProperty(
                    colorList = arrayListOf(colorList[index]),
                    isHighlight = isHighlight,
                    highColor = highColor,
                    valueSize = valueSize,
                    valueColor = colorList[index],
                    formatter = formatter
                )
        }
    ).apply {
        when (entries.size) {
            1 -> {
                barWidth = barWidthRatio
                xAxis.setCenterAxisLabels(false)
            }
            else -> {
                barWidth = BigDecimal(1.0 / (entries.size + 1))
                    .setScale(2, BigDecimal.ROUND_DOWN)
                    .toFloat()
                groupSpace = 1 - barWidth * entries.size
                groupBars(xFromValue, groupSpace, 0f)
            }
        }
    }

    if (entries.size > 1) {
        xAxis.axisMaximum =
            xFromValue + barData.getGroupWidth(groupSpace, 0f) * entries[0].size
    }

    // 设置为可以左右滑动，放大x轴倍数
    // viewPortHandler.refresh(Matrix().apply { postScale(1.5f, 1f) }, this, false)
}

/**
 * 设置 BarChart stack 数据
 *
 * @param entries       堆叠数据
 * @param colorList     堆块颜色
 * @param labelList     图例文本
 * @param isHighlight   是否高亮
 * @param highColor     高亮颜色
 * @param barWidthRatio 柱状宽比例
 * @param valueSize     参考值文本
 * @param valueColor    参考值颜色
 */
fun <T : BarChart> T.setStackData(
    entries: List<BarEntry>,
    colorList: List<Int>,
    labelList: Array<String> = arrayOf(),
    isHighlight: Boolean = true,
    highColor: Int = Color.TRANSPARENT,
    barWidthRatio: Float = 0.4f,
    valueSize: Float = 10f,
    valueColor: Int = context.getColorEx(R.color.black),
    formatter: IValueFormatter? = null
) {
    if (data != null && data.dataSetCount > 0) {
        (data.getDataSetByIndex(0) as BarDataSet).entries = entries
        data.notifyDataChanged()
        notifyDataSetChanged()
    } else {
        data = BarData(
            BarDataSet(entries, "")
                .setProperty(
                    colorList = colorList,
                    stackList = labelList,
                    isHighlight = isHighlight,
                    highColor = highColor,
                    valueSize = valueSize,
                    valueColor = valueColor,
                    formatter = formatter
                )
        ).apply {
            barWidth = barWidthRatio //柱块宽度比例
        }
    }

    // 设置为可以左右滑动，放大x轴倍数
    // viewPortHandler.refresh(Matrix().apply { postScale(1.5f, 1f) }, this, false)
}

/**
 * 设置 BarChart BarDataSet属性
 *
 * @param colorList     柱子颜色
 * @param stackList     图例标签文本
 * @param isHighlight   是否高亮
 * @param highColor     高亮颜色
 * @param valueSize     参考值文本
 * @param valueColor    参考值颜色
 */
fun BarDataSet.setProperty(
    colorList: List<Int> = ArrayList(),
    stackList: Array<String> = arrayOf(),
    isHighlight: Boolean = false,
    highColor: Int = Color.TRANSPARENT,
    valueSize: Float = 10f,
    valueColor: Int = -1,
    formatter: IValueFormatter? = null
) = apply {
    colors = colorList      //设置柱子颜色，循环使用
    stackLabels = stackList //图例标签文本

    isHighlightEnabled = isHighlight //选中柱子是否高亮显示
    highLightColor = highColor       //高亮颜色

    valueTextSize = valueSize   //柱块描述文本大小
    valueTextColor = valueColor //柱块描述文本颜色
    valueFormatter = formatter  //柱块描述文本格式化
}

/**
 * 设置单组 LineChart 数据
 */
fun <T : LineChart> T.setSingleData(
    entries: List<Entry>,
    lineMode: String = "",
    lineSize: Float = 2f,
    lineColor: Int = -1,
    isDrawCircles: Boolean = true,
    labelRadius: Float = 3f,
    labelColor: Int = -1,
    isDrawHole: Boolean = false,
    holeRadius: Float = 2f,
    holeColor: Int = -1,
    highColor: Int = Color.TRANSPARENT,
    isDrawFill: Boolean = true,
    @DrawableRes drawableFill: Int = -1,
    valueSize: Float = 10f,
    valueColor: Int = context.getColorEx(R.color.black),
    formatter: IValueFormatter? = null
) {
    if (data != null && data.dataSetCount > 0) {
        (data.dataSets[0] as LineDataSet).entries = entries
        data.notifyDataChanged()
        notifyDataSetChanged()
    } else {
        data = LineData(
            LineDataSet(entries, "").apply {
                axisDependency = YAxis.AxisDependency.LEFT //参考坐标位置，左或右

                //参考点连接方式，曲线、直线、折线等
                mode = when (lineMode) {
                    "linear" -> LineDataSet.Mode.LINEAR
                    "step" -> LineDataSet.Mode.STEPPED
                    "cubic" -> LineDataSet.Mode.CUBIC_BEZIER
                    else -> LineDataSet.Mode.HORIZONTAL_BEZIER
                }

                lineWidth = lineSize //连线宽度
                color = lineColor    //连线颜色

                lineWidth = lineSize //连线宽度
                color = lineColor    //连线颜色

                setDrawCircles(isDrawCircles) //是否显示圆点
                circleRadius = labelRadius    //参圆点直径
                setCircleColor(labelColor)    //参圆点颜色

                setDrawCircleHole(isDrawHole) //参考点是否为空心圆
                circleHoleRadius = holeRadius //空心圆直径
                circleHoleColor = holeColor   //空心圆颜色

                highLightColor = highColor                         //高亮颜色
                setDrawFilled(isDrawFill)                          //连线内是否填充
                fillDrawable = context.getDrawableEx(drawableFill) //连线内填充颜色
            }
        ).apply {
            setValueTextSize(valueSize)   //参考点描述文本大小
            setValueTextColor(valueColor) //参考点描述文本颜色
            setValueFormatter(formatter)  //参考点描述文本格式
        }
    }

    // 设置为可以左右滑动，放大x轴倍数
    // viewPortHandler.refresh(Matrix().apply { postScale(1.5f, 1f) }, this, false)
}

/**
 * 设置 LineChart LineDataSet属性
 */
fun LineDataSet.setProperty(
    lineMode: String = "",
    lineSize: Float = 2f,
    lineColor: Int = -1,
    isDrawCircles: Boolean = true,
    labelRadius: Float = 3f,
    labelColor: Int = -1,
    isDrawHole: Boolean = false,
    holeRadius: Float = 2f,
    holeColor: Int = -1,
    highColor: Int = Color.TRANSPARENT,
    isDrawFill: Boolean = true,
    drawableFill: Drawable? = null,
    valueSize: Float = 10f,
    valueColor: Int = -1,
    formatter: IValueFormatter? = null
) = apply {
    axisDependency = YAxis.AxisDependency.LEFT //参考坐标位置，左或右

    //参考点连接方式，曲线、直线、折线等
    mode = when (lineMode) {
        "linear" -> LineDataSet.Mode.LINEAR
        "step" -> LineDataSet.Mode.STEPPED
        "horizontal" -> LineDataSet.Mode.HORIZONTAL_BEZIER
        else -> LineDataSet.Mode.CUBIC_BEZIER
    }

    lineWidth = lineSize //连线宽度
    color = lineColor    //连线颜色

    lineWidth = lineSize //连线宽度
    color = lineColor    //连线颜色

    setDrawCircles(isDrawCircles) //是否显示圆点
    circleRadius = labelRadius    //参圆点直径
    setCircleColor(labelColor)    //参圆点颜色

    setDrawCircleHole(isDrawHole) //参考点是否为空心圆
    circleHoleRadius = holeRadius //空心圆直径
    circleHoleColor = holeColor   //空心圆颜色

    highLightColor = highColor  //高亮颜色
    setDrawFilled(isDrawFill)   //连线内是否填充
    fillDrawable = drawableFill //连线内填充颜色

    valueTextSize = valueSize   //标签文本大小
    valueTextColor = valueColor //标签文本颜色
    valueFormatter = formatter  //标签文本格式化
}